package com.aiti.lulian.service.impl;

import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.lulian.dto.LuLianRecruitmentDto;
import com.aiti.lulian.entity.LuLianRecruitment;
import com.aiti.lulian.mapper.LuLianRecruitmentMapper;
import com.aiti.lulian.service.ILuLianRecruitmentService;
import com.aiti.lulian.vo.LuLianRecruitmentVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author WongMz
 * @CreateDate 2024-08-14
 * @Description: （技术改造、数字化转型、海外贸易）招募单服务层实现类
 */
@Service
public class LuLianRecruitmentServiceImpl extends BaseSerivceImpl<LuLianRecruitmentMapper, LuLianRecruitment> implements ILuLianRecruitmentService {

    @Autowired
    private LuLianRecruitmentMapper recruitmentMapper;

    @Override
    public Map<String, Object> queryList(Page<LuLianRecruitment> page, LuLianRecruitment recruitment) {
        Map<String, Object> map = new HashMap<>();

        String createDate = null;
        if (recruitment.getBaseCreateTime() != null) {
            Date baseCreateTime = recruitment.getBaseCreateTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            createDate = format.format(baseCreateTime);
        }
        List<LuLianRecruitment> list = recruitmentMapper.selectRecruitmentPage(page, recruitment, createDate);

        map.put("total", page.getTotal());
        map.put("list", list);

        return map;
    }

    @Override
    public Map<String, Object> queryAllList(Page<LuLianRecruitmentVo> page, LuLianRecruitment recruitment) {
        Map<String, Object> map = new HashMap<>();

        String signupDeadlineDateStr = null;
        if (recruitment.getSignupDeadlineDate() != null) {
            signupDeadlineDateStr = new SimpleDateFormat("yyyy-MM-dd").format(recruitment.getSignupDeadlineDate());
        }
        List<LuLianRecruitmentVo> list = recruitmentMapper.selectAllList(page, recruitment, signupDeadlineDateStr);

        map.put("total", page.getTotal());
        map.put("list", list);

        return map;
    }

    @Override
    public Map<String, Object> queryAllListBackend(Page<LuLianRecruitmentVo> page, LuLianRecruitmentDto recruitmentDto) {
        Map<String, Object> map = new HashMap<>();

        List<LuLianRecruitmentVo> list = recruitmentMapper.selectAllListByUserId(page, recruitmentDto);

        map.put("total", page.getTotal());
        map.put("list", list);

        return map;
    }

    @Override
    public Map<String, Object> queryCount(String userId) {
        Map<String, Object> map = new HashMap<>();

        //招募中总数
        Integer isNotExpiredCount = recruitmentMapper.selectCountByCondition(LuLianRecruitmentDto.builder().isExpired("0").creator(userId).build());
        //已截止总数
        Integer isExpiredCount = recruitmentMapper.selectCountByCondition(LuLianRecruitmentDto.builder().isExpired("1").creator(userId).build());
        //已停用总数
        Integer isDelCount = recruitmentMapper.selectCountByCondition(LuLianRecruitmentDto.builder().isDel("1").creator(userId).build());

        map.put("isNotExpiredCount", isNotExpiredCount);
        map.put("isExpiredCount", isExpiredCount);
        map.put("isDelCount", isDelCount);

        return map;
    }
}